package com.molyfun.parents.activity.common.attachment;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.TeacherDetailResponse;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/28/16.
 */
public class TeacherViewer extends RecyclerView {

    private final int TYPE_TEACHER = 1001;
    private final int TYPE_MORE = 1002;

    private List<TeacherDetailResponse.TeacherDetailBean> mTeacherList;

    private Context context;
    LinearLayoutManager mLinearLayoutManager;
    private TeacherAdapter mTeacherAdapter;

    public TeacherViewer(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public TeacherViewer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TeacherViewer(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        mLinearLayoutManager = new LinearLayoutManager(context);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        setLayoutManager(mLinearLayoutManager);
        setHasFixedSize(true);

        mTeacherList = new ArrayList<>();
        mTeacherAdapter = new TeacherAdapter();
        setAdapter(mTeacherAdapter);
    }

    public void initData(List<TeacherDetailResponse.TeacherDetailBean> attachmentViewList) {
        mTeacherList = attachmentViewList;
        mTeacherAdapter.notifyDataSetChanged();
    }

    public class TeacherAdapter extends Adapter<ViewHolder> {

        public TeacherAdapter() {
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            switch (viewType){
                case TYPE_TEACHER:
                    return new AttachmentViewHolder(LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.layout_teacher_cell, parent, false));
                case TYPE_MORE:
                    default:
                    return new TeacherMoreViewHolder(LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.layout_teacher_more, parent, false));
            }
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            switch (getItemViewType(position)){
                case TYPE_TEACHER:
                    AttachmentViewHolder attachmentViewHolder = (AttachmentViewHolder) holder;
                    final TeacherDetailResponse.TeacherDetailBean teacherDetailBean = mTeacherList.get(position);

                    if (!TextUtils.isEmpty(teacherDetailBean.getHeadimgurl())) {
                        Picasso.with(getContext())
                                .load(teacherDetailBean.getHeadimgurl())
                                .config(Bitmap.Config.RGB_565)
                                .placeholder(R.mipmap.ic_header_default)
                                .into(attachmentViewHolder.mAvator);
                    } else {
                        attachmentViewHolder.mAvator.setImageResource(R.mipmap.ic_header_default);
                    }

                    attachmentViewHolder.mTeacherName.setText(teacherDetailBean.getUsername());
                    break;
                case TYPE_MORE:
                    TeacherMoreViewHolder teacherMoreViewHolder = (TeacherMoreViewHolder) holder;

                    break;

            }

        }

        @Override
        public int getItemViewType(int position) {
            if (position < 3){
                return TYPE_TEACHER;
            }else{
                return TYPE_MORE;
            }
        }

        @Override
        public int getItemCount() {
            if (mTeacherList == null) {
                return 0;
            }
            if(mTeacherList.size() > 3){
                return 4;
            }else{
                return mTeacherList.size();
            }
        }

        public class AttachmentViewHolder extends ViewHolder {

            @InjectView(R.id.avator)
            CircleImageView mAvator;
            @InjectView(R.id.teacher_name)
            TextView mTeacherName;

            public AttachmentViewHolder(View view) {
                super(view);
                ButterKnife.inject(this, view);
            }
        }

        public class TeacherMoreViewHolder extends ViewHolder {

            public TeacherMoreViewHolder(View view) {
                super(view);
                ButterKnife.inject(this, view);
            }
        }

    }

}